package com.vrv.im.control;

import java.util.HashMap;
import java.util.Map;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.util.StringUtils;
import org.springframework.validation.BindingResult;
import org.springframework.validation.Errors;
import org.springframework.validation.Validator;
import org.springframework.web.bind.WebDataBinder;
import org.springframework.web.bind.annotation.InitBinder;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.servlet.ModelAndView;

import com.vrv.im.domain.UserBean;
import com.vrv.im.service.ProjectService;
import com.vrv.im.service.RoleService;
import com.vrv.im.service.UserRoleService;
import com.vrv.im.service.UserService;
import com.vrv.im.validate.UserValidate;

@Controller
@RequestMapping("/bind")
public class UserBindController {

	@Autowired
	private UserService userService;
	@Autowired
	private ProjectService projectService;
	@Autowired
	private RoleService roleService;
	@Autowired
	private UserRoleService userRoleService;

	private Validator validator;

	public UserBindController() {
		super();
		validator = new UserValidate();
	}

	@InitBinder
	public void initBinder(WebDataBinder binder) {
		binder.setValidator(validator);
	}

	@RequestMapping("/goToBind")
	public ModelAndView goToBindPage(@ModelAttribute("voModel") UserBean userBean, Errors errors, BindingResult bindingResult) {

		// validator.validate(userBean, errors);
		if (userBean == null || !StringUtils.hasText(userBean.getOpenId()) || !StringUtils.hasText(userBean.getToken())) {
			ModelAndView mav = new ModelAndView("userbind/bind");
			mav.addObject("voModel", new UserBean());
			mav.addObject("type", "error");
			mav.addObject("errotMsg", "请传递参数openID，tocken");
			return mav;
		} else {
			UserBean ub = userService.getUserByTokenID(userBean);
			if (ub == null || ub.getDelete()) {
				ModelAndView mav = new ModelAndView("userbind/bind");
				mav.addObject("voModel", userBean);
				mav.addObject("type", "info");
				mav.addObject("infoMsg", "请绑定微信帐号");
				return mav;
			} else {
				// session.setAttribute("userBean", ub);
				// return new ModelAndView("redirect:/progress/goToProgress.do");
				Map<String, String> varMap = new HashMap<String, String>();
				varMap.put("openId", ub.getOpenId());
				varMap.put("token", ub.getToken());
				return new ModelAndView("redirect:/bindProject/myProject.do", varMap);
			}
		}
	}

	@RequestMapping("/goToUnBind")
	public ModelAndView goToUnBind(@ModelAttribute("voModel") UserBean userBean, Errors errors, BindingResult bindingResult) {
		if (userBean == null || !StringUtils.hasText(userBean.getOpenId()) || !StringUtils.hasText(userBean.getToken())) {
			ModelAndView mav = new ModelAndView("userbind/unBind");
			mav.addObject("voModel", new UserBean());
			mav.addObject("type", "error");
			mav.addObject("errotMsg", "请传递参数openID，tocken");
			return mav;
		} else {
			ModelAndView mav = new ModelAndView("userbind/unBind");
			userBean = userService.getUserByTokenID(userBean);
			mav.addObject("voModel", userBean);
			mav.addObject("type", "info");
			return mav;
		}
	}

	@RequestMapping("/unBind")
	public ModelAndView unBind(@ModelAttribute("voModel") UserBean userBean, Errors errors, BindingResult bindingResult) {
		if (userBean == null || !StringUtils.hasText(userBean.getOpenId()) || !StringUtils.hasText(userBean.getToken())) {
			ModelAndView mav = new ModelAndView("redirect:/bind/goToUnBind.do");
			mav.addObject("voModel", new UserBean());
			mav.addObject("type", "error");
			mav.addObject("errotMsg", "请传递参数openID，tocken");
			return mav;
		} else {

			userService.removeUser(userBean);

			ModelAndView mav = new ModelAndView("redirect:/bind/goToBind.do");
			mav.addObject("voModel", userBean);
			mav.addObject("type", "info");
			mav.addObject("infoMsg", "请绑定微信帐号");
			return mav;
		}
	}

	@RequestMapping("/bindUser")
	public ModelAndView bindUser(@ModelAttribute("voModel") UserBean userBean, Errors errors, BindingResult bindingResult) {
		validator.validate(userBean, errors);
		if (bindingResult.hasErrors()) {
			ModelAndView mav = new ModelAndView("userbind/bind");
			mav.addObject("voModel", new UserBean());
			mav.addObject("type", "error");
			mav.addObject("errotMsg", "表单内容均为必填");
			return mav;
		} else {
			@SuppressWarnings("unused")
			long userID = userService.bindUser(userBean);
			return new ModelAndView("redirect:/bindProject/goToBindProject.do");
		}
	}

	// @RequestMapping("/bindUser")
	// public ModelAndView bindUserProject(@ModelAttribute("user") UserVo userVo, HttpSession session) {
	// UserBean ub = new UserBean();
	// if (userVo != null) {
	// long userID = userService.addUser(userVo);
	// ub.setId(userID);
	// }
	// ub.setToken(userVo.getToken());
	// ub.setName(userVo.getName());
	// ub.setPhone(userVo.getPhone());
	// if (session.getAttribute("userBean") == null)
	// session.setAttribute("userBean", ub);
	// return new ModelAndView("redirect:/progress/goToProgress.do");
	// }

}
